<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

	<title>Form unit test</title>
	<style type="text/css">
		@import "../../../dojo/resources/dojo.css";
		@import "../css/dijitTests.css";
	</style>
	<script type="text/javascript" src="../../../dojo/dojo.js"
		djConfig="isDebug: true, parseOnLoad: true"></script>
	<script type="text/javascript" src="../_testCommon.js"></script>

	<script type="text/javascript">
		dojo.require("doh.runner");
		dojo.require("dojo.date");
		dojo.require("dijit.form.Form");
		dojo.require("dijit.layout.LayoutContainer");
		dojo.require("dijit.layout.ContentPane");
		dojo.require("dijit.form.ComboBox");
		dojo.require("dijit.form.CheckBox");
		dojo.require("dijit.form.DateTextBox");
		dojo.require("dijit.form.Button");
		dojo.require("dijit.form.MultiSelect");
		dojo.require("dijit.form.Textarea");
		dojo.require("dijit.form.SimpleTextarea");
		dojo.require("dijit.Editor");

		var obj;
		function getValues(){
			obj = dijit.byId('myForm').getValues();
			console.log("Object is: " + dojo.toJson(obj, true));
		}
		
		function setValues(){
			if(!obj){
		 		obj = {testF: 'testi'};
			}
			console.log("Object is: " + dojo.toJson(obj, true));
			dijit.byId('myForm').setValues(obj);
		}

		// make dojo.toJson() print dates correctly (this feels a bit dirty)
		Date.prototype.json = function(){ return dojo.date.stamp.toISOString(this, {selector: 'date'});};

		var d = dojo.date.stamp.fromISOString;

		// These are the values assigned to the widgets in the page's HTML
		var original =	{
							foo: {bar: {baz: {quux: d("2007-12-30")} } },
							available: {from: d("2005-01-02"), to: d("2006-01-02")},
							plop: {combo: "one"},
							cb2: ["2", "3"],
							r2: "2",
							ms1: ["VA", "WA"],
							h1: "hidden",
							t1: "line 1\nline 2",
							st1: "simple line 1\nsimple line 2",
							richtext: "<h1>original</h1><p>This is the default content</p>",
							filename: ""
						};

		// we change the form to these values
		var changed =	{
							foo: {bar: {baz: {quux: d("2005-01-01")} } },
							available: {from: d("2005-11-02"), to: d("2006-11-02")},
							plop: {combo: "three"},
							cb2: ["4"],
							r2: "1",
							ms1: ["FL", "CA"],
							h1: "still hidden",
							t1: "new line 1\nnew line 2",
							st1: "new simple line 1\nnew simple line 2",
							richtext: "<h1>changed</h1><p>This is the changed content set by setValues</p>",
							filename: ""
						};
		// we reset the form to these values
		var reset =	{
							foo: {bar: {baz: {quux: d("2007-12-30")} } },
							available: {from: d("2005-01-02"), to: d("2006-01-02")},
							plop: {combo: "one"},
							cb2: ["2", "3"],
							r2: "2",
							ms1: ["VA", "WA"],
							h1: "hidden",
							t1: "line 1\nline 2",
							st1: "simple line 1\nsimple line 2",
							richtext: "<h1>changed</h1><p>This is the changed content set by setValues</p>", // not a form element, so not reset
							filename: ""
						};

		dojo.addOnLoad(function(){
			doh.register("dijit.form.Form",
				[
					function getValues(){
						var values = dijit.byId("myForm").getValues();
						
						// FF3 sticks in some tabs and newlines that mess up the equality check
						// Need better way to compare two HTML trees but for now do this.
						values.richtext = values.richtext.replace(/[\n\t]/, "", "g");

						doh.is( dojo.toJson(original), dojo.toJson(values) );
					},
					function setValues(){
						dijit.byId("myForm").setValues(changed);
						doh.is( dojo.toJson(changed), dojo.toJson(dijit.byId("myForm").getValues()) );
					},
					function nameAttributeSurvived(){  // ticket:4753
						var radios = dojo.query(".RadioButton", dijit.byId("radio-cells")).forEach(
							function(r) {
								doh.is( r.inputNode.name, "r2" );
							});
						
					},
					function resetTest(){
						dijit.byId("myForm").reset();
						doh.is( dojo.toJson(reset), dojo.toJson(dijit.byId("myForm").getValues()) );
					}
				]
			);
			doh.run();
		});

	</script>
</head>
<body>
	<h1>Form Widget Unit Test</h1>
	<p>
		The form widget takes data in a form and serializes/deserializes it, so
		it can be submitted as a JSON string of nested objects.
	</p>
	<div style="color:red">Currently only widgets are supported, not raw elements.</div>
	<!--    to test form submission, you'll need to create an action handler similar to
			http://www.utexas.edu/teamweb/cgi-bin/generic.cgi 
		http://www.tipjar.com/cgi-bin/test -->
	<form dojoType="dijit.form.Form" id="myForm" 
		encType="multipart/form-data" action="" method="">
		<script type="dojo/method" event="onReset">
			return confirm('Press OK to reset widget values');
		</script>
		<script type="dojo/method" event="onSubmit">
			console.debug('Attempting to submit form w/values:\n',
				dojo.toJson(this.getValues(),true)
			);
			if(this.validate()){
				return confirm('Form is valid, press OK to submit');
			}else{
				alert('Form contains invalid data.  Please correct first');
				return false;
			}
			return true;
		</script>
		<script type="dojo/method" event="onReset">
			return confirm('reset Form?');
		</script>
		<p>Just HTML text</p>
		<table style="border: 1px solid #9f9f9f;" cellspacing="10">
			<thead>
				<tr>
					<th>Description</th>
					<th>Name</th>
					<th>Form node/widget</th>
				</tr>
			</thead>
			<tbody>
				<!--
				<tr><td>text</td><td>testF</td><td><input type="text" name="testF" value="bar1" /></td></tr>
				<tr><td>password</td><td>passwordF</td><td><input type="password" name="passwordF" value="bar4" /></td></tr>
				<tr><td>hidden</td><td>hiddenF</td><td><input type="hidden" name="hiddenF" value="bar4" /></td></tr>
				<tr><td>select</td><td>plop.noncombo</td><td>
				<div class="group">
					<select name="plop.noncombo">
					 <option value="1">one</option>
					 <option value="2">two</option>
					 <option value="3">three</option>
					</select>
				</div>

				</td></tr>
				-->

				<tr>
					<td>DateTextBox inside contentpane</td>
					<td>foo.bar.baz.quux</td>
					<td>
						<div dojoType="dijit.layout.ContentPane">
						<input type="text" name="foo.bar.baz.quux" dojoType="dijit.form.DateTextBox" value="2007-12-30" />
						</div>
					</td>
				</tr>
				<tr>
					<td>Layoutcontainer</td>
					<td>
						<div dojoType="dijit.layout.LayoutContainer"></div>
					</td>
				</tr>
				<tr>
					<td>DateTextBox 1</td>
					<td>available.from</td>
					<td>
						<input type="text" name="available.from" dojoType="dijit.form.DateTextBox" value="2005-01-02" />
					</td>
				</tr>
				<tr>
					<td>DateTextBox 2</td>
					<td>available.to</td>
					<td>
						<input type="text" name="available.to" dojoType="dijit.form.DateTextBox" value="2006-01-02" />
					</td>
				</tr>
				<tr>
					<td>ComboBox</td>
					<td>plop.combo</td>
					<td>
						<select name="plop.combo" dojoType="dijit.form.ComboBox">
							<option value="one">one</option>
							<option value="two">two</option>
							<option value="three">three</option>
						</select>
					</td>
				</tr>

				<!--
				<tr>
					<td>textarea</td>
					<td>myTextArea</td>
					<td>
						<textarea name="myTextArea">
							text text text """ \\\/
						</textarea>
					</td>
				</tr>
				-->

				<!--
				<tr>
					<td>CheckBox</td>
					<td>cb1</td>
					<td>
						<input type="checkbox" name="cb1" value="1" /> 1
						<input type="checkbox" name="cb1" value="2" checked="checked" /> 2
						<input type="checkbox" name="cb1" value="3" checked="checked" /> 3
						<input type="checkbox" name="cb1" value="4" /> 4
					</td>
				</tr>
				-->

				<tr>
					<td>CheckBox widget</td>
					<td>cb2</td>
					<td>
						<input dojoType="dijit.form.CheckBox" type="checkbox" name="cb2" value="1" /> 1
						<input dojoType="dijit.form.CheckBox" type="checkbox" name="cb2" value="2" checked="checked" /> 2
						<input dojoType="dijit.form.CheckBox" type="checkbox" name="cb2" value="3" checked="checked" /> 3
						<input dojoType="dijit.form.CheckBox" type="checkbox" name="cb2" value="4" /> 4
					</td>
				</tr>

				<!--
				<tr>
					<td>radio</td>
					<td>r1</td>
					<td>
						<input type="radio" name="r1" value="1" /> 1
						<input type="radio" name="r1" value="2" /> 2
						<input type="radio" name="r1" value="3" /> 3
						<input type="radio" name="r1" value="4" /> 4
					</td>
				</tr>
				-->

				<tr>
				<td>Radio widget</td><td>r2</td>
				<td id="radio-cells">
				<input dojoType="dijit.form.RadioButton" type="radio" name="r2" value="1" /> 1
				<input dojoType="dijit.form.RadioButton" type="radio" name="r2" value="2" checked="checked" /> 2
				<input dojoType="dijit.form.RadioButton" type="radio" name="r2" value="3"/> 3
				<input dojoType="dijit.form.RadioButton" type="radio" name="r2" value="4" /> 4
				</td>
				</tr>

				<tr>
					<td>Multi-select</td><td>ms1</td>
					<td>
						<select id="ms1" multiple="true" name="ms1"
							dojoType="dijit.form.MultiSelect"
							style="height:100px; width:175px; border:5px solid #ededed;">

							<option value="TN">Tennessee</option>
							<option value="VA" selected="true">Virginia</option>
							<option value="WA" selected="true">Washington</option>
							<option value="FL">Florida</option>
							<option value="CA">California</option>

						</select>
					</td>
				</tr>

				<tr>
					<td>Hidden input</td>
					<td>h1</td>
					<td>
						<input id="h1" name="h1" dojoType="dijit.form.TextBox" type="hidden" value="hidden">
					</td>
				</tr>

				<tr>
					<td>Auto-sizing textarea</td>
					<td>t1</td>
					<td>
						<textarea id="t1" name="t1" 
	dojoType="dijit.form.Textarea">line 1
line 2</textarea>
					</td>
				</tr>

				<tr>
					<td>Fixed size textarea</td>
					<td>st1</td>
					<td>
						<textarea id="st1" name="st1" dojoType="dijit.form.SimpleTextarea" rows=5 cols=50>
simple line 1
simple line 2</textarea>
					</td>
				</tr>

				<tr>
					<td>Editor widget</td>
					<td>richtext</td>
					<td>
						<textarea dojoType="dijit.Editor" name="richtext" pluginsConfig="[{items:['bold','italic']}]"><h1>original</h1><p>This is the default content</p></textarea>
					</td>
				</tr>

				<tr>
					<td>File upload</td>
					<td>filename</td>
					<td>
						<input dojoType="dijit.form.TextBox" name="filename" type="file">
					</td>
				</tr>
			</tbody>
		</table>

		<button dojoType=dijit.form.Button onClick="getValues();">Get Values from form!</button>
		<button dojoType=dijit.form.Button onClick="setValues();">Set Values to form!</button>
		<button dojoType=dijit.form.Button type="submit">Submit</button>
		<button dojoType=dijit.form.Button type="reset">Reset</button>
	</form>


</body>
</html>
